package com.a.lib_base.base.ui

import android.app.Application
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.a.lib_base.able.IBaseViewModel
import com.a.lib_base.utils.BaseViewModelAssist

/**
 * 抽象的ViewModel_Activity
 * 提供了ViewModel 获取方法，可通过IBaseViewModel实现
 */
abstract class BaseViewModelActivity<VB : ViewBinding> : BaseActivityViewBinding<VB>(),
    IBaseViewModel {

    @JvmField
    protected val viewModelAssist = BaseViewModelAssist()

    // =====================
    // = Activity Provider =
    // =====================

    override fun <T : ViewModel> getActivityViewModel(modelClass: Class<T>): T {
        return viewModelAssist.getActivityViewModelCache(this, modelClass)
    }

    override fun <T : ViewModel> getFragmentViewModel(modelClass: Class<T>): T? {
        return null
    }

// TODO: 2022/7/27 未测试过
    // ========================
    // = Application Provider =
    // ========================

    override fun <T : ViewModel> getAppViewModel(
        application: Application?,
        modelClass: Class<T>
    ): T? {
        return viewModelAssist.getAppViewModel(application, modelClass)
    }

    override fun getAppViewModelProvider(application: Application?): ViewModelProvider? {
        return viewModelAssist.getAppViewModelProvider(application)
    }

    override fun getAppFactory(application: Application?): ViewModelProvider.Factory? {
        return viewModelAssist.getAppFactory(application)
    }


}